﻿using Microsoft.AspNetCore.Mvc;
using XinLife.Core.Helper;
using XinLife.Core.Model;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace XinLife.Controllers.Business
{
    /// <summary>
    /// 图片工具类
    /// </summary>
    public class ImageToolController : BaseController
    {
        private readonly ILogger<ImageToolController> _logger;
        public ImageToolController(ILogger<ImageToolController> logger)
        {
            _logger = logger;
        }

        [HttpPost]
        public AjaxResult ImgToBase64([FromForm] string path)
        {
            var baseDirec = Directory.GetCurrentDirectory();
            var rootPath = Path.Combine(baseDirec, path);
            if (!System.IO.File.Exists(rootPath))
            {
                return Error("未找到文件");
            }
            else
            {
                string filepath = Path.Combine(baseDirec, "webfile");
                FileInfo fileInfo = new FileInfo(rootPath);
                if (!fileInfo.FullName.Contains(filepath))
                {
                    return Error("非法路径!");
                }
            }
            string base64 = ImageUtil.FileToBase64(rootPath);
            return Success(base64);
        }
    }
}
